package cn.edu.zust.se.gateway.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    private static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    // 设置缓存数据
    public static void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 设置缓存数据并设置过期时间
    public static void setWithExpire(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 获取缓存数据
    public static Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除缓存数据
    public static void delete(String key) {
        redisTemplate.delete(key);
    }

    // 检查缓存中是否存在某个 key
    public static boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
}
